import { Request, Response, NextFunction } from "express";
import path from "path";
import fs from "fs";
import feifanc from "../util/feifanc";
import { img } from "../config";

export default async (req: Request, res: Response, next: NextFunction) => {
  if (img.includes(path.extname(req.path))) {
    return next();
  }

  const pathName = path.join(__dirname, "./log.json");
  const token = req.signedCookies.feifan;
  const data = {
    method: req.method,
    reqUrl: req.path,
    user: token ? token.user : "游客",
    body: req.body,
    query: req.query,
    time: feifanc.date("all"),
  };
  next();
  await fs.promises.appendFile(pathName, JSON.stringify(data) + ",");
};
